package com.hzqy.service.epg;

import com.hzqy.persistence.dao.epg.AcLotteryDao;
import com.hzqy.web.vo.AcWinningVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AcLotteryService {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private AcLotteryDao acDao;
	public  Map<String,Object> selectAc(AcWinningVo alv, int page, int rows){
        Map<String,Object> map=new HashMap<String, Object>();
        // page：页码，起始值 1。
        //rows：每页显示行。
        Integer rowCount = acDao.selectAcCount(alv);
        if(rowCount>0 && page>0 && rows>0){
            map.put("total", rowCount);
            map.put("f_awl_serverid",alv.getF_awl_serverid());
            map.put("f_awl_prizename",alv.getF_awl_prizename());
            map.put("f_awl_activityid",alv.getF_awl_activityid());
            map.put("f_awl_type",alv.getF_awl_type());
            map.put("beginDate",alv.getBeginDate());
            map.put("endDate",alv.getEndDate());
            map.put("firstResult", (page-1)*rows);
            map.put("maxResults", rows);
            List<AcWinningVo> list=acDao.selectAc(map);
            map.put("rows", list);
        }else{
            map.put("rows", new ArrayList<AcWinningVo>());
        }
        return map;
	}
    /**
     * 增加内容---单条入库都利用此方法
     * @param acWinningVo
     * @return
     */
    public Map<String,Object> insertWinningService(AcWinningVo acWinningVo) {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        int flag = 0;
        if(acWinningVo==null || acWinningVo.getF_awl_serverid()==null||acWinningVo.getF_awl_userphone()==null||acWinningVo.getF_awl_prizename()==null) {
            //不满足基本数据条件
            flag = -1;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<AcWinningVo> AcWinningVos = acDao.selectByRequest(acWinningVo);
        resultMap.put("f_awl_prizename",acWinningVo.getF_awl_prizename());
        resultMap.put("f_awl_serverid",acWinningVo.getF_awl_serverid());
        resultMap.put("f_awl_date",acWinningVo.getF_awl_date());
        resultMap.put("f_awl_userphone",acWinningVo.getF_awl_userphone());
        if(acWinningVo.getF_awl_activityid()!=null){
            resultMap.put("f_awl_activityid",acWinningVo.getF_awl_activityid());
        }else{
            resultMap.put("f_awl_activityid",null);
        }
        if(acWinningVo.getF_awl_prizeid()!=null){
            resultMap.put("f_awl_prizeid",acWinningVo.getF_awl_prizeid());
        }else{
            resultMap.put("f_awl_prizeid",null);
        }
         if(acWinningVo.getF_ui_resolution()!=0){
              resultMap.put("f_ui_resolution",acWinningVo.getF_ui_resolution());
         } else{
             resultMap.put("f_ui_resolution",null);
         }
        if(flag ==0&&AcWinningVos.size()==0) {//直接插入数据
            try {
                acDao.insertAcWinningList(resultMap);
                resultMap.put("success", true);
                resultMap.put("result", "新增内容成功!");
            }catch (Exception e){
                resultMap.put("success", false);
                resultMap.put("result","新增内容失败!");
            }
        }else{//失败
            resultMap.put("success", false);
            resultMap.put("result","重复新增内容失败!");
        }
        return resultMap;
    }
    /**
	 *  根据id查询
	 * @return
	 */
    public AcWinningVo selectAcWinningById(int f_awl_id) {
    	return acDao.selectAcWinningById(f_awl_id);
    }
    /**
	 * 根据id删除
	 * @return
	 */
    public Map<String,Object> deleteAcWinning(int f_awl_id) {
    	Map<String,Object> resultMap = new HashMap<String,Object>();
    	int flag= acDao.deleteAcWinning(f_awl_id);
    	if(flag>0){
    		resultMap.put("success", true);
	   		resultMap.put("result", "修改成功!");
	   	}else {
	   		resultMap.put("success", false);
	   		resultMap.put("result", "修改失败!");
	   	}
    	return resultMap;
    }
    /**
	 * 根据id修改
	 * @param awv
	 * @return
	 */
    public Map<String,Object> updateAcWinning(AcWinningVo awv) {
    	Map<String,Object> resultMap = new HashMap<String,Object>();
    	int flag= acDao.updateAcWinning(awv);
    	if(flag>0){
    		 resultMap.put("success", true);
             resultMap.put("result", "修改成功!");
    	}else {
    		resultMap.put("success", false);
            resultMap.put("result", "修改失败!");
    	}
    	
    	return resultMap;
    }

}
